import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes: RouteRecordRaw[] = [

  {
    path: '/index',
    component: Layout,
    redirect: '/index/dashboard',
    meta: { title: '系统管理', icon: 'setting' },
    children: [
      
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: { title: '首页' }
      }
    ]
  },
  
  {
    path: '/furniture',
    component: Layout,
    meta: { title: '家具管理', icon: 'Files' },
    children: [
      {
        path: 'list',
        name: 'FurnitureList',
        component: () => import('@/views/furniture/list.vue'),
        meta: { title: '家具列表' }
      },
      {
        path: 'category',
        name: 'Category',
        component: () => import('@/views/furniture/category.vue'),
        meta: { title: '分类管理' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', icon: 'ShoppingCart' },
    children: [
      {
        path: 'list',
        name: 'OrderList',
        component: () => import('@/views/order/list.vue'),
        meta: { title: '订单列表' }
      },
      {
        path: 'after-sale',
        name: 'AfterSale',
        component: () => import('@/views/order/after-sale.vue'),
        meta: { title: '售后管理' }
      }
    ]
  },
  {
    path: '/warehouse',
    component: Layout,
    meta: { title: '仓库管理', icon: 'Box' },
    children: [
      {
        path: 'stock',
        name: 'Stock',
        component: () => import('@/views/warehouse/stock.vue'),
        meta: { title: '库存管理' }
      },
      {
        path: 'in-out',
        name: 'InOut',
        component: () => import('@/views/warehouse/in-out.vue'),
        meta: { title: '出入库记录' }
      }
    ]
  },
  {
    path: '/delivery',
    component: Layout,
    meta: { title: '送货管理', icon: 'Van' },
    children: [
      {
        path: 'task',
        name: 'DeliveryTask',
        component: () => import('@/views/delivery/task.vue'),
        meta: { title: '送货任务' }
      },
      {
        path: 'driver',
        name: 'DeliveryDriver',
        component: () => import('@/views/delivery/driver.vue'),
        meta: { title: '司机管理' }
      },
      {
        path: 'route',
        name: 'DeliveryRoute',
        component: () => import('@/views/delivery/route.vue'),
        meta: { title: '路线规划' }
      },
      {
        path: 'track',
        name: 'DeliveryTrack',
        component: () => import('@/views/delivery/track.vue'),
        meta: { title: '配送跟踪' }
      }
    ]
  },
  {
    path: '/',redirect: '/index/dashboard'
  },
 
  // 添加404路由
  {
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/error/404.vue')
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 添加全局错误处理
router.onError((error) => {
  console.error('Router error:', error)
  // 可以在这里添加错误处理逻辑，比如重定向到错误页面
})

// 添加导航守卫
router.beforeEach((to, from, next) => {
  // 这里可以添加加载状态
  next()
})

export default router 